package com.yanqu.road.server.cmd.relatives;

import com.yanqu.road.entity.relatives.RelativesInfo;
import com.yanqu.road.entity.relatives.RelativesLetter;
import com.yanqu.road.pb.relatives.RelativesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.server.manger.relatives.RelativesMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_RELATIVES_SEE_LETTER, desc = "查看书信")
public class SeeLetterCmd extends AbstractCommand {

    private long relativesId;
    private long letterId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RelativesProto.SeeLetterReqMsg reqMsg = RelativesProto.SeeLetterReqMsg.parseFrom(packet.getBytes());
        relativesId = reqMsg.getRelativesId();
        letterId = reqMsg.getLetterId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        RelativesInfo relativesInfo = player.getModule(RelativesModule.class).getRelativesInfo(relativesId);
        if (relativesInfo == null || relativesInfo.getStatus() == 0) {
            ret = GameErrorCode.E_RELATIVES_TIMES_NOT_ENOUGH;
            return;
        }

        RelativesLetter letter = player.getModule(RelativesModule.class).getLetter(relativesId, letterId);
        if (letter == null) {
            ret = GameErrorCode.E_RELATIVES_NOT_FIND_LETTER;
            return;
        }

        RelativesMgr.seeLetter(letterId);
    }

    @Override
    public void sendData(GamePlayer player) {
        RelativesProto.SeeLetterRespMsg.Builder respMsg = RelativesProto.SeeLetterRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_RELATIVES_SEE_LETTER, respMsg);
    }
}
